<?php


namespace app\common\cachemodel\zl2;


use app\common\cachemodel\Article;
use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class Zl2FamousPersonRecode extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl2_famous_person_recode';

    protected $cacheModel = 'synastry.famous_person_recode';

    public function getAllIds()
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc')
            {
                try
                {
                    $data = $this->where(['status' => 1])->order("id {$sortType}")->column('id');

                    $resData = [];
                    foreach ($data as $id)
                    {
                        $resData[$id] = ['id' => $id];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return $resData['data'];
    }

    public function getListDatas()
    {
        request()->get(['page' => -1]);
        $data = $this->getListPage('', 'id', ['id', 'synastry_num', 'article_id', 'img', 'content', 'name', 'user_des'], 0, 'desc', 0);

        $resData = [];
        foreach ($data['data'] as $datum)
        {
            $articleInfo = null;
            if ($datum['article_id'])
            {
                $artInfo = current((new Article())->batchGetInfo([$datum['article_id']], ['id', 'cids', 'read', 'read_base', 'thumb', 'title', 'publish_time', 'author', 'avatar', 'content', 'introduction', 'comment', 'good', 'collect', 'a_id', 'author_type']));
                $readNum = ($artInfo['read'] ?? 0) + ($artInfo['read_base'] ?? 0);

                $articleInfo = [
                    'read_num' => $readNum,
                    'title' => $artInfo['title'],
                    'target' => 'local',
                    'link' => "article_details?id={$datum['article_id']}&need_auth=0",
                ];
            }
            $resData[] = [
                'id' => (int)$datum['id'],
                'name' => $datum['name'],
                'img' => $datum['img'],
                'desc' => $datum['user_des'],
                'content' => $datum['content'],
                'synastry_num' => $datum['synastry_num'],
                'article_info' => $articleInfo,
            ];
        }

        return $resData;
    }

    /**
     *
     * @param int $id
     * @return array
     */
    public function getInfoData(int $id)
    {
        $detail = $this->getInfoById($id, ['id', 'synastry_num', 'article_id', 'img', 'content', 'name', 'user_des']);

        $articleInfo = null;
        if ($detail['article_id'])
        {
            $artInfo = current((new Article())->batchGetInfo([$detail['article_id']], ['id', 'cids', 'read', 'read_base', 'thumb', 'title', 'publish_time', 'author', 'avatar', 'content', 'introduction', 'comment', 'good', 'collect', 'a_id', 'author_type']));
            $readNum = ($artInfo['read'] ?? 0) + ($artInfo['read_base'] ?? 0);

            $articleInfo = [
                'read_num' => $readNum,
                'title' => $artInfo['title'],
                'target' => 'local',
                'link' => "article_details?id={$detail['article_id']}&need_auth=0",
            ];
        }
        $resData[] = [
            'id' => (int)$detail['id'],
            'name' => $detail['name'],
            'img' => $detail['img'],
            'desc' => $detail['user_des'],
            'content' => $detail['content'],
            'synastry_num' => $detail['synastry_num'],
            'article_info' => $articleInfo,
        ];

        return $resData;
    }

    /**
     * 更新数据
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function updateData(int $id, array $data)
    {
        try
        {
            // 更新数据
            $this->where(['id' => $id])->update($data);
            // 更新缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Zl2FamousPersonRecode model => UpdateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }

    /**
     * 批量获取 名人档案数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $data = $this->where(['id' => ['in', $ids]])->order("id {$sortType}")->column('status,extra,synastry_num,article_id,img,content,create_time', 'id');
            } else
            {
                $data = $this->where(['status' => 1])->order("id {$sortType}")->column('status,extra,synastry_num,article_id,img,content,create_time', 'id');
            }

            foreach ($data as $id => $datum)
            {
                $name = '';
                $userDes = '';
                if ($datum['extra'])
                {
                    $extend = json_decode($datum['extra'], true);
                    $userDes = $extend['user_des'] ?? '';
                    $name = $extend['name'];
                }
                $data[$id]['name'] = $name;
                $data[$id]['user_des'] = $userDes;
            }

        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'status', 'synastry_num', 'article_id', 'create_time']);

            if (isset($itemData['extra']))
            {
                $data[$index]['extra'] = json_decode($itemData['extra'], true);
            }
        }

        return $data;
    }


}